package org.misty.blog.dao;

import org.misty.blog.po.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long>, JpaSpecificationExecutor<Article> {
    @Query("from Article t where t.recommend = true and t.published = true")
    List<Article> listTop(Pageable pageable);

    Page<Article> findAllByTagsIdIn(Iterable<Long> tagIds, Pageable pageable);

    @Query("select function('date_format', a.updateTime, '%Y') as year from Article a where a.published = true group by function('date_format', a.updateTime, '%Y') order by a.updateTime desc ")
    List<String> findGroupYear();

    @Query("select a from Article a where a.published = true and function('date_format', a.updateTime, '%Y') = ?1 order by a.updateTime desc ")
    List<Article> findByYear(String year);
}
